package com.fiberhome.nas.core.database.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.fiberhome.nas.core.database.entity.UploadTaskEntity
import kotlinx.coroutines.flow.Flow

@Dao
interface UploadTaskDao {

    /**
     * 插入上传任务项
     * 如果已存在相同主键的记录，则替换
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertUploadTask(task: UploadTaskEntity)

    /**
     * 批量插入上传任务
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertUploadTasks(tasks: List<UploadTaskEntity>)

    /**
     * 更新上传任务
     */
    @Update
    suspend fun updateUploadTask(task: UploadTaskEntity)

    /**
     * 删除上传任务
     */
    @Delete
    suspend fun deleteUploadTask(task: UploadTaskEntity)

    /**
     * 根据ID删除上传任务
     */
    @Query("DELETE FROM upload_tasks WHERE id = :taskId")
    suspend fun deleteUploadTaskById(taskId: String)

    /**
     * 获取所有上传任务
     */
    @Query("SELECT * FROM upload_tasks")
    fun getAllUploadTasks(): Flow<List<UploadTaskEntity>>

    /**
     * 获取未完成的上传任务（等待中、上传中、暂停）
     */
    @Query("SELECT * FROM upload_tasks WHERE status IN ('Waiting', 'Uploading', 'Paused')")
    fun getUnfinishedUploadTasks(): Flow<List<UploadTaskEntity>>

    /**
     * 获取已完成的上传任务
     */
    @Query("SELECT * FROM upload_tasks WHERE status = 'Completed' ")
    fun getCompletedUploadTasks(): Flow<List<UploadTaskEntity>>

    /**
     * 获取失败的上传任务
     */
    @Query("SELECT * FROM upload_tasks WHERE status = 'Failed'")
    fun getFailedUploadTasks(): Flow<List<UploadTaskEntity>>

    /**
     * 根据ID获取上传任务
     */
    @Query("SELECT * FROM upload_tasks WHERE id = :taskId")
    suspend fun getUploadTaskById(taskId: String): UploadTaskEntity?
}